package com.application.chiettinhdienluc.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.application.chiettinhdienluc.app.MainApplication;
import com.application.chiettinhdienluc.model.NguoiDungModel;

public class NhanVienDataSource {
	private final MySQLiteHelper dbHelper;

	public static final String TABLE_NGUOIDUNG = "D_NGUOIDUNG";
	public static final String COLUMN_TEN_DANG_NHAP = "TEN_DANG_NHAP";
	public static final String COLUMN_TEN_NGUOI_DUNG = "TEN_NGUOI_DUNG";
	public static final String COLUMN_MAT_KHAU = "MAT_KHAU";
	public static final String COLUMN_PHAN_QUYEN = "PHAN_QUYEN";
	public static final String COLUMN_MA_DVIQLY = "MA_DVIQLY";
	public static final String COLUMN_MA_NVIEN = "MA_NVIEN";
	private final static String TAG = NhanVienDataSource.class.getSimpleName();

	public NhanVienDataSource() {
		dbHelper = MySQLiteHelper.getInstance();
	}

	public boolean checkNguoiDungExist(String maNVien) {

		boolean existed = false;
		Cursor c = null;
		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				c = dbHelper.getDb().rawQuery("select rowid from "
						+ TABLE_NGUOIDUNG + " where MA_NVIEN = " 
						+ "'" + maNVien  + "'", null);		

				if (c != null && c.getCount() > 0){
					existed = true;			
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally{
			if(c!=null)
			{
				c.close();
				c = null;
			}
		}

		return existed;	
	}


	public NguoiDungModel getLoginInfo(String username, String password)
	{
		Cursor c = null;

		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				String rawQuery;

				if (MainApplication.getMaDonViQuanLy().equalsIgnoreCase(""))
				{
					rawQuery = "select * from " + TABLE_NGUOIDUNG + " where TEN_DANG_NHAP = '" + username + "' and " +  " MAT_KHAU = '" + password + "'";
				} 
				else
				{
					//rawQuery = "select * from " + TABLE_NGUOIDUNG + " where TEN_DANG_NHAP = '" + username + "' and " +  " MAT_KHAU = '" + password + "' " + " and MA_DVIQLY = '" + MainApplication.getMaDonViQuanLy() + "'";

					rawQuery = "select * from " + TABLE_NGUOIDUNG + " where TEN_DANG_NHAP = '" + username + "' and " +  " MAT_KHAU = '" + password + "' ";
				}        

				c = dbHelper.getDb().rawQuery(rawQuery, null);	        	

				if (c != null && c.getCount() > 0) {

					int colId_TenDangNhap = c.getColumnIndex(COLUMN_TEN_DANG_NHAP);
					int colId_TenNguoidung = c.getColumnIndex(COLUMN_TEN_NGUOI_DUNG);
					int colId_MatKhau = c.getColumnIndex(COLUMN_MAT_KHAU);
					int colId_PhanQuyen = c.getColumnIndex(COLUMN_PHAN_QUYEN);
					int colId_MaDV = c.getColumnIndex(COLUMN_MA_DVIQLY);
					int colId_MaNV = c.getColumnIndex(COLUMN_MA_NVIEN);

					c.moveToFirst();

					String tenDangNhap = c.getString(colId_TenDangNhap);
					String tenNguoidung = c.getString(colId_TenNguoidung);
					String matKhau = c.getString(colId_MatKhau);
					String phanQuyen= c.getString(colId_PhanQuyen);
					String maDV = c.getString(colId_MaDV);
					String maNV = c.getString(colId_MaNV);

					NguoiDungModel nd = new NguoiDungModel();

					nd.set_TenDangNhap(tenDangNhap);
					nd.set_TenNguoiDung(tenNguoidung);
					nd.set_MatKhau(matKhau);
					nd.set_PhanQuyen(phanQuyen);
					nd.set_MaDonViQuanLy(maDV);
					nd.set_MaNVien(maNV);

					return nd;
				}
				else
				{
					return null;
				}
			}	
		} catch (Exception e) {
		}
		finally{
			if(c!=null)
			{
				c.close();
				c = null;
			}
		}

		return null;
	}

	public long insert(NguoiDungModel nguoidung) {

		long row_id = -1;

		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				ContentValues cv = new ContentValues();

				cv.put(COLUMN_TEN_DANG_NHAP, nguoidung.get_TenDangNhap());
				cv.put(COLUMN_TEN_NGUOI_DUNG, nguoidung.get_TenNguoiDung());
				cv.put(COLUMN_MAT_KHAU, nguoidung.get_MatKhau());
				cv.put(COLUMN_PHAN_QUYEN, nguoidung.get_PhanQuyen());
				cv.put(COLUMN_MA_DVIQLY, nguoidung.get_MaDonViQuanLy());
				cv.put(COLUMN_MA_NVIEN, nguoidung.get_MaNVien());

				row_id = dbHelper.getDb().insert(TABLE_NGUOIDUNG, null, cv);
			}	
		} catch (Exception e) {
		}
		finally{
		}
		return row_id;

	}

	public int update(NguoiDungModel nguoidung) {

		int numRowEffect = 0;

		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				ContentValues cv = new ContentValues();

				cv.put(COLUMN_TEN_DANG_NHAP, nguoidung.get_TenDangNhap());
				cv.put(COLUMN_TEN_NGUOI_DUNG, nguoidung.get_TenNguoiDung());
				cv.put(COLUMN_MAT_KHAU, nguoidung.get_MatKhau());
				cv.put(COLUMN_PHAN_QUYEN, nguoidung.get_PhanQuyen());
				cv.put(COLUMN_MA_DVIQLY, nguoidung.get_MaDonViQuanLy());
				cv.put(COLUMN_MA_NVIEN, nguoidung.get_MaNVien());

				String whereClause = "TEN_DANG_NHAP" + "=?" + " and " + "TEN_NGUOI_DUNG" + "=?" + " and " + "MAT_KHAU" + "=?" + " and " + "PHAN_QUYEN" + "=?" + " and " + "MA_DVIQLY" + "=?";
				String[] whereArgs = new String[] { nguoidung.get_TenDangNhap(), nguoidung.get_TenNguoiDung(), nguoidung.get_MatKhau(), nguoidung.get_PhanQuyen(), nguoidung.get_MaDonViQuanLy() };

				numRowEffect = dbHelper.getDb().update(TABLE_NGUOIDUNG, cv, whereClause, whereArgs);
			}	
		} catch (Exception e) {
		}
		finally{
		}

		return numRowEffect;
	}
	
	public int update(String username, String dpn, String pass, int caseUpdate) {

		int numRowEffect = 0;

		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				ContentValues cv = new ContentValues();

				if(caseUpdate == 1)
					cv.put(COLUMN_TEN_NGUOI_DUNG, dpn);
				else if(caseUpdate == 2)
					cv.put(COLUMN_MAT_KHAU, pass);
				else if(caseUpdate == 3)
				{
					cv.put(COLUMN_TEN_NGUOI_DUNG, dpn);
					cv.put(COLUMN_MAT_KHAU, pass);
				}

				String whereClause = "TEN_DANG_NHAP" + " = ? ";
				String[] whereArgs = new String[] { username };

				numRowEffect = dbHelper.getDb().update(TABLE_NGUOIDUNG, cv, whereClause, whereArgs);
			}	
		} catch (Exception e) {
		}
		finally{
		}

		return numRowEffect;
	}

	public ArrayList<NguoiDungModel> loadNguoidung() {
		ArrayList<NguoiDungModel> listnguoidungModel = new ArrayList<NguoiDungModel>();

		Cursor c = null;

		try {

			if(dbHelper != null && dbHelper.getDb() != null)
			{
				c = dbHelper.getDb().rawQuery("select * from " + TABLE_NGUOIDUNG, null);

				if (c != null && c.getCount() > 0) {
					int colId_TenDangNhap = c.getColumnIndex(COLUMN_TEN_DANG_NHAP);
					int colId_TenNguoidung = c.getColumnIndex(COLUMN_TEN_NGUOI_DUNG);
					int colId_MatKhau = c.getColumnIndex(COLUMN_MAT_KHAU);
					int colId_PhanQuyen = c.getColumnIndex(COLUMN_PHAN_QUYEN);
					int colId_MaDV = c.getColumnIndex(COLUMN_MA_DVIQLY);
					int colId_MaNV = c.getColumnIndex(COLUMN_MA_NVIEN);

					c.moveToFirst();
					do {
						try {
							String tenDangNhap = c.getString(colId_TenDangNhap);
							String tenNguoidung = c.getString(colId_TenNguoidung);
							String matKhau = c.getString(colId_MatKhau);
							String phanQuyen= c.getString(colId_PhanQuyen);
							String maDV = c.getString(colId_MaDV);
							String maNV = c.getString(colId_MaNV);

							NguoiDungModel nc = new NguoiDungModel();

							nc.set_TenDangNhap(tenDangNhap);
							nc.set_TenNguoiDung(tenNguoidung);
							nc.set_MatKhau(matKhau);
							nc.set_PhanQuyen(phanQuyen);
							nc.set_MaDonViQuanLy(maDV);
							nc.set_MaNVien(maNV);

							listnguoidungModel.add(nc);
						} catch (Exception e) {
						}
					} while (c.moveToNext());
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally{
			if(c!=null)
			{
				c.close();
				c = null;
			}
		}

		return listnguoidungModel;
	}

}
